﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TheSimGer
{
    public enum BuffType 
    { 
        /// <summary>
        /// 跳转到下一天的时候，额外触发的事件
        /// </summary>
        Day,
        /// <summary>
        /// 每秒刷新的BUFF类型
        /// </summary>
        Tick,
        None,
    }

    /// <summary>
    /// BUFF类型
    /// </summary>
    public enum BuffZhengFu 
    {
        Positive,
        Negative,
        Normal, //中性BUFF
        None,
    }

    [Serializable()]
    public class TheBuff : Prefab
    {
        public int TTL { get; set; } = 0;
        public int TTLDay { get; set; } = 0;
        public BuffType BT { get; set; } = BuffType.None;
        public BuffZhengFu BZF { get; set; } = BuffZhengFu.None; //BUFF的类型是正面还是负面

        /// <summary>
        /// BUFF的任务标签
        /// </summary>
        public List<string> _QuestTag { get; set; } = new List<string>();
        /// <summary>
        /// 每一个扫描周期经过
        /// </summary>
        public Action _OnTick { get; set; } = null;
        /// <summary>
        /// 每天经过
        /// </summary>
        public Action _OnDayPassed { get; set; } = null;

        /// <summary>
        /// 每个事件经过
        /// </summary>
        public Action _OnEventPassed { get; set; } = null;
    }

    /// <summary>
    /// Buff管理
    /// </summary>
    public class BuffHelper 
    {
        /// <summary>
        /// 随机BUFF生成器
        /// </summary>
        public static TheBuff GenRandom(List<Action> Data) 
        {
            var RR = new TheBuff();
            RR.Name = new List<string> { "随机Buff1", "随机Buff2", "随机Buff2" }.GetRandom();
            //随机Buff时间范围
            if (TheRandom.Range(1, 1000) < 500) RR.BT = BuffType.Day; else RR.BT = BuffType.Tick;
            //随机决定正负面
            if (TheRandom.Range(1, 1000) < 500) RR.BZF = BuffZhengFu.Positive; else RR.BZF = BuffZhengFu.Negative;

            if (RR.BT == BuffType.Day)
            {
                RR.TTLDay = TheRandom.Range(1, 5);
            }
            else if (RR.BT == BuffType.Tick) 
            {
                RR.TTL = TheRandom.Range(1, 360);
            }

            if (Data.Count >= 3)
            {
                RR._OnTick = Data[0];
                RR._OnDayPassed = Data[1];
                RR._OnEventPassed = Data[2];
            }
            else 
            {
                if (RR.BZF == BuffZhengFu.Negative)
                {
                    if (RR.BT == BuffType.Tick)
                    {
                        var RandomRank = TheRandom.Range(1, 5);

                        if (RandomRank == 1)
                        {
                            RR._OnTick = new Action(() =>
                            {
                                PlayerDongCount.TheState.CurrentMoney -= TheRandom.Range(1, 3);
                            });
                        }
                        else if (RandomRank == 2)
                        {
                            RR._OnTick = new Action(() =>
                            {
                                PlayerDongCount.TheState.ShiWu -= TheRandom.Range(1, 3);
                            });
                        }
                        else if (RandomRank == 3)
                        {
                            RR._OnTick = new Action(() =>
                            {
                                PlayerDongCount.TheState.YuLe -= TheRandom.Range(1, 3);
                            });
                        }
                        else if (RandomRank == 4)
                        {
                            RR._OnTick = new Action(() =>
                            {
                                PlayerDongCount.TheState.XiuXi -= TheRandom.Range(1, 3);
                            });
                        }
                        else if (RandomRank == 5)
                        {
                            RR._OnTick = new Action(() =>
                            {
                                PlayerDongCount.TheState.NengLi -= TheRandom.Range(1, 3);
                            });
                        }
                    }
                    else if (RR.BT == BuffType.Day)
                    {
                        var RandomRank = TheRandom.Range(1, 5);

                        if (RandomRank == 1)
                        {
                            RR._OnDayPassed = new Action(() =>
                            {
                                PlayerDongCount.TheState.CurrentMoney -= TheRandom.Range(1, 600);
                            });
                        }
                        else if (RandomRank == 2)
                        {
                            RR._OnDayPassed = new Action(() =>
                            {
                                PlayerDongCount.TheState.ShiWu -= TheRandom.Range(1, 30);
                            });
                        }
                        else if (RandomRank == 3)
                        {
                            RR._OnDayPassed = new Action(() =>
                            {
                                PlayerDongCount.TheState.YuLe -= TheRandom.Range(1, 30);
                            });
                        }
                        else if (RandomRank == 4)
                        {
                            RR._OnDayPassed = new Action(() =>
                            {
                                PlayerDongCount.TheState.XiuXi -= TheRandom.Range(1, 30);
                            });
                        }
                        else if (RandomRank == 5)
                        {
                            RR._OnDayPassed = new Action(() =>
                            {
                                PlayerDongCount.TheState.NengLi -= TheRandom.Range(1, 30);
                            });
                        }
                    }
                }
                else if (RR.BZF == BuffZhengFu.Positive) 
                {
                    if (RR.BT == BuffType.Tick)
                    {
                        var RandomRank = TheRandom.Range(1, 5);

                        if (RandomRank == 1)
                        {
                            RR._OnTick = new Action(() =>
                            {
                                PlayerDongCount.TheState.CurrentMoney += TheRandom.Range(1, 3);
                            });
                        }
                        else if (RandomRank == 2)
                        {
                            RR._OnTick = new Action(() =>
                            {
                                PlayerDongCount.TheState.ShiWu += TheRandom.Range(1, 3);
                            });
                        }
                        else if (RandomRank == 3)
                        {
                            RR._OnTick = new Action(() =>
                            {
                                PlayerDongCount.TheState.YuLe += TheRandom.Range(1, 3);
                            });
                        }
                        else if (RandomRank == 4)
                        {
                            RR._OnTick = new Action(() =>
                            {
                                PlayerDongCount.TheState.XiuXi += TheRandom.Range(1, 3);
                            });
                        }
                        else if (RandomRank == 5)
                        {
                            RR._OnTick = new Action(() =>
                            {
                                PlayerDongCount.TheState.NengLi += TheRandom.Range(1, 3);
                            });
                        }
                    }
                    else if (RR.BT == BuffType.Day)
                    {
                        var RandomRank = TheRandom.Range(1, 5);

                        if (RandomRank == 1)
                        {
                            RR._OnDayPassed = new Action(() =>
                            {
                                PlayerDongCount.TheState.CurrentMoney += TheRandom.Range(1, 300);
                            });
                        }
                        else if (RandomRank == 2)
                        {
                            RR._OnDayPassed = new Action(() =>
                            {
                                PlayerDongCount.TheState.ShiWu += TheRandom.Range(1, 30);
                            });
                        }
                        else if (RandomRank == 3)
                        {
                            RR._OnDayPassed = new Action(() =>
                            {
                                PlayerDongCount.TheState.YuLe += TheRandom.Range(1, 30);
                            });
                        }
                        else if (RandomRank == 4)
                        {
                            RR._OnDayPassed = new Action(() =>
                            {
                                PlayerDongCount.TheState.XiuXi += TheRandom.Range(1, 30);
                            });
                        }
                        else if (RandomRank == 5)
                        {
                            RR._OnDayPassed = new Action(() =>
                            {
                                PlayerDongCount.TheState.NengLi += TheRandom.Range(1, 30);
                            });
                        }
                    }
                }
            }

            return RR;
        }
    }
}
